<?php

error_reporting(E_ALL);

class Formulaire
{
	private $object_form;
	private $nb_fields;
	private $form_name;
	private $style_class;
	private $name_hidden_field;
	private $all_post;
	private $action_form;
	
	// CONSTRUCTEUR
	
	public function __construct($fname, $style, $action_form = "")
	{
		$this->nb_fields = 0; // Nombre de champs generer
		
		$this->form_name = $fname; // Nom de la form
		
		$this->prefix_name = $fname."_"; // Prefixe de tous les champs pour renforcer l'efficacité de la compilation du nom des champs
		
		$this->style_class = $style; // Style du formulaire
		
		$this->name_hidden_field = md5($this->prefix_name."valid"); // Champ caché pour la validation
		
		$this->action_form = (empty($action_form)) ? htmlentities($_SERVER['PHP_SELF']) : $action_form; // Destination du formulaire
	}
	
	// METHODES PUBLIQUES D'IMPRESSION DE CHAMPS
	
	public function printInput($name, $size, $maxlength, $value = "")
	{		
		$value = ($this->getValue($name) == "") ? $value : $this->getValue($name);
		
		return $this->ajouteObjet($this->genInput($name, $size, $maxlength, $value), $name);
	}
	
	public function printInputPass($name, $size, $maxlength)
	{	
		return $this->ajouteObjet($this->genInputPass($name, $size, $maxlength), $name);
	}
	
	public function printCaptcha()
	{	
		return $this->ajouteObjet($this->genCaptcha(), "captcha");
	}
	
	public function printTextArea($name, $cols, $rows, $value = "")
	{		
		$value = ($this->getValue($name) == "") ? $value : $this->getValue($name);
		
		return $this->ajouteObjet($this->genTextArea($name, $cols, $rows, $value), $name);
	}
	
	public function printSubmit($value = "Valider")
	{		
		return $this->ajouteObjet($this->genSubmitBtn($value), "submit");
	}
	
	public function printSelect($name, $options, $selected_value = "")
	{		
		$selected_value = ($this->getValue($name) == "") ? $selected_value : $this->getValue($name);
		
		return $this->ajouteObjet($this->genSelect($name, $options, $selected_value), $name);
	}
	
	public function printRadio($name, $options, $checked_value = "", $retour_ligne = false)
	{		
		$checked_value = ($this->getValue($name) == "") ? $checked_value : $this->getValue($name);
		
		return $this->ajouteObjet($this->genRadio($name, $options, $checked_value, $retour_ligne), $name);
	}
	
	public function printBegin()
	{		
		return $this->ajouteObjet($this->genFormBegin(), $this->form_name);
	}
	
	public function printEnd()
	{
		return $this->ajouteObjet($this->genFormEnd(), "End");
	}
	
	
	// METHODES PUBLIQUES APRES VALIDATION DU FORMULAIRE
	
	public function formValid($post)
	{
		$this->all_post = $post;
		return isset($this->all_post[$this->name_hidden_field]) ? true : false;
	}
	
	public function getValue($name)
	{
		return isset($this->all_post[md5($this->prefix_name.$name)]) ? $this->nettoieChaine($this->all_post[md5($this->prefix_name.$name)]) : null;
	}
	
	
	// METHODES PUBLIQUES DE VERIFICATION
	
	public function verifEmail($email)
	{
		return filter_var($email, FILTER_VALIDATE_EMAIL) === FALSE ? false : true;
	}
	
	public function verifUrl($url) // Verifie la presence du http://
	{
		return filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_SCHEME_REQUIRED) === FALSE ? false : true;
	}
	
	public function verifNumeric($num, $size) // Champs numerique et taille correcte
	{
		return ctype_digit($num) and strlen($num) == $size ? true : false;
	}
	
	public function verifCaptcha()
	{
		return $_SESSION['captcha'] == strtolower($this->all_post[md5($this->prefix_name.'captcha')]) ? true : false;
	}
	
	
	// METHODES PRIVEES
	
	private function objetExist($name)
	{
		$i = 0;
		while (($i < $this->nb_fields) and ($name <> $this->object_form[$i]['name'])) $i++;
		
		if ($i == $this->nb_fields) return false;
		else return true;
	}
	
	private function ajouteObjet($obj, $name)
	{
		$name = $this->prefix_name.$name;
		if (!$this->objetExist($name))
		{
			$this->object_form[$this->nb_fields]['name'] = $name;
			$this->nb_fields += 1;
			return $obj;
		}
	}
	
	private function nettoieChaine($chaine)
	{
		return trim(strip_tags(stripslashes(htmlentities(str_replace("\\", "", $chaine)))));
	}
	
	
	
	// METHODES PRIVEES => GENERATEUR DE HTML
	
	private function genInput($name, $size, $maxlength, $value)
	{
		$name = md5($this->prefix_name.$name);
		return "<input type=\"input\" name=\"$name\" value=\"$value\" size=\"$size\" maxlength=\"$maxlength\">";
	}
	
	private function genInputPass($name, $size, $maxlength)
	{
		$name = md5($this->prefix_name.$name);
		return "<input type=\"password\" name=\"$name\" size=\"$size\" maxlength=\"$maxlength\">";
	}
	
	private function genTextArea($name, $cols, $rows, $value)
	{
		$name = md5($this->prefix_name.$name);
		return "<textarea name=\"$name\" cols=\"$cols\" rows=\"$rows\">$value</textarea>";
	}
	
	private function genSelect($name, $options, $selected_value)
	{
		$name = md5($this->prefix_name.$name);
		
		$retour = "<select name=\"$name\" class=\"".$this->style_class."\">";
		foreach($options as $val => $aff)
		{
			$selected = $selected_value == $val ? "selected=\"selected\"" : "";
			$retour .= "<option value=\"$val\" $selected>$aff</option>";
		}
		$retour .= "</select>";
		
		return $retour;
	}
	
	private function genRadio($name, $options, $check_value, $retour_ligne)
	{
		$name = md5($this->prefix_name.$name);
		$retour = "";
		
		$br = $retour_ligne ? "<br />" : "";
		
		foreach($options as $val => $aff)
		{
			$check = $check_value == $val ? "checked" : "";
			$retour .= "<label><input type=\"radio\" name=\"$name\" value=\"$val\" $check>$aff</label>$br";
		}
		
		return $retour;
	}
	
	private function genSubmitBtn($value)
	{
		return "<input type=\"submit\" value=\"$value\">";
	}
	
	private function genCaptcha()
	{
		$_SESSION['captcha'] = substr(md5(rand(0, 100000).time()), 0, 6);
		return "<img src=\"res/captcha.php\"><br />".$this->genInput("captcha", "12", "6", "");
	}
	
	private function genFormBegin()
	{
		$begin = "<form action=\"".$this->action_form."\" method=\"post\" class=\"".$this->style_class."\">";
		$begin .= "<input type=\"hidden\" name=\"".$this->name_hidden_field."\" value=\"1\">";
		return $begin;
	}
	
	private function genFormEnd()
	{
		return "</form>";
	}
}

?>